home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14163 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Compare Function
  5. Date: 12 Apr 1996 07:04:12 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4kkv9c$6q4@sparcserver.lrz-muenchen.de>
  9. References: <4kk5jv$41o@freenet-news.carleton.ca>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. aq436@FreeNet.Carleton.CA (Jerry Boyd) writes:
  13.  
  14. >How would I write a compare function (compare_strings, for 
  15. >instance) that uses character pointers.  
  16. >This is how the compare_strings function should look, but need to 
  17. >implement using pointers.
  18. >int compare_strings (char s1[], char s2[])
  19. int compare_strings(char *s1, char *s2)
  20. >{
  21. >  int i = 0, answer
  22. int i = 0, answer;
  23. >    
  24. >  while ( s1[i] == s2[i] && s1[i]  != '\0' && s2[i] != '\0' )
  25. while(*(s1 + i) == *(s2 + i) && *(s1 + i) != '\0' && *(s2 + i) != '\0')
  26.  
  27. >  ++i;
  28. >  if ( s1[i] < s2[i] )
  29. if (*(s1 + i) < *(s2 + i))
  30. >      answer = -1;
  31. >  else if ( s1[i] == s2[i] )
  32. else if (*(s1 + i) == *(s2 + i))
  33. >      answer = 0;
  34. >  else
  35. >      answer = 1;               /*  s1 > s2   */
  36. >  return (answer);
  37. >}
  38.  
  39. Because "foo[bar]" is just a different notation for "*((foo) + (bar))",
  40. your program already uses pointers, so maybe what you want to use are
  41. '*' characters?
  42.  
  43. BTW, there is a function called strcmp() in the standard C library
  44. that might meet your specification quite well.
  45.  
  46. Kurt
  47. -- 
  48. | Kurt Watzka                             Phone : +49-89-2180-6254
  49. | watzka@stat.uni-muenchen.de
  50.